<template>
  <div>
    <el-button
      type="primary"
      :disabled="!categoryStore.c3Id"
      icon="Plus"
      @click="addSpu"
    >
      添加
    </el-button>
    <el-table :data="tableData" :border="true">
      <el-table-column type="index" label="序号" width="80" />
      <el-table-column prop="spuName" label="名称" width="120" />
      <el-table-column prop="description" label="描述" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            type="primary"
            size="small"
            icon="Plus"
            @click="addSku(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            @click="editSpu(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="View"
            @click="findSku(row)"
          ></el-button>

          <el-popconfirm
            :title="`确认删除${row.spuName}`"
            width="200px"
            @confirm="deleteSku(row)"
          >
            <template #reference>
              <el-button type="danger" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="tableData.length"
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 7, 9]"
      :background="true"
      layout=" prev, pager, next, jumper,->,total, sizes"
      :total="total"
      @size-change="changeSize"
      @current-change="getSpu"
    />

    <el-dialog v-model="dialogVisible" title="SKU列表">
      <el-table :data="skuArr" :border="true">
        <el-table-column prop="skuName" label="名字" width="180" />
        <el-table-column prop="price" label="价格" width="180" />
        <el-table-column prop="weight" label="重量" />
        <el-table-column label="图片">
          <template #default="{ row }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { watch, ref } from 'vue'
import { reqHasSpu, reqSkuList, reqRemoveSpu } from '@/api/product/spu/index'
import type {
  Records,
  SkuData,
  HasSpuResponseData,
  SpuData,
  SkuInfoData
} from '@/api/product/spu/type'
import useCategoryStore from '@/store/modules/category'
const categoryStore = useCategoryStore()
const pageNo = ref<number>(1)
const pageSize = ref<number>(3)
const total = ref<number>(0)
const tableData = ref<Records>([])
watch(
  () => categoryStore.c3Id,
  (c3Id) => {
    tableData.value = []

    if (!c3Id) return

    getSpu()
  }
)

const getSpu = async (pager = 1) => {
  pageNo.value = pager
  const result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id
  )
  tableData.value = result.data.records
  total.value = result.data.total
}

const changeSize = () => {
  getSpu()
}
const $emit = defineEmits(['addSpu', 'editSpu', 'addSku', 'removeSpu'])
const addSpu = () => {
  $emit('addSpu')
}
const editSpu = (row: SpuData) => {
  $emit('editSpu', row)
}
const addSku = (row: SpuData) => {
  $emit('addSku', row)
}
const removeSpu = (row: SpuData) => {
  $emit('removeSpu', row)
}

const skuArr = ref<SkuData[]>([])
const findSku = async (row: SpuData) => {
  const result: SkuInfoData = await reqSkuList(row.id as number)
  skuArr.value = result.data
  dialogVisible.value = true
}
const dialogVisible = ref<boolean>(false)
const deleteSku = async (row: SpuData) => {
  await reqRemoveSpu(row.id as number)
  ElMessage({
    type: 'success',
    message: '删除成功'
  })
  removeSpu(row)
}
defineExpose({ getSpu, pageNo ,tableData})
</script>

<style scoped></style>
